/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Desktop_Payment.java
 *
 * Created on Apr 4, 2011, 10:02:48 AM
 */
package DesktopGUIpackage;

import Controller.DesktopController;
import Controller.DesktopControllerHelper;
import DTO.Address;
import DTO.Payment;



/**
 *
 * @author edaype
 */
public class Desktop_Payment extends javax.swing.JPanel {

    private DesktopController desktopController;
    private DesktopControllerHelper desktopControllerHelper;

    private String cardType;
    private boolean cardSelected;

    /** Creates new form Desktop_Payment */
    public Desktop_Payment(DesktopControllerHelper desktopControllerHelper) {
        initComponents();
        this.desktopControllerHelper = desktopControllerHelper;
        desktopController = desktopControllerHelper.getDesktopController();
        cardType = "";
        cardSelected = false;
        ErrorLabel.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        CardPanel = new javax.swing.JPanel();
        CardTitleLabel = new javax.swing.JLabel();
        CardnumberLabel = new javax.swing.JLabel();
        CardNameLabel = new javax.swing.JLabel();
        CardSecurityLabel = new javax.swing.JLabel();
        CardExpirationLabel = new javax.swing.JLabel();
        CardNumberLabel = new javax.swing.JTextField();
        CardNameField = new javax.swing.JTextField();
        CardSecurityField = new javax.swing.JTextField();
        CardImageLabel = new javax.swing.JLabel();
        CardMonthCombo = new javax.swing.JComboBox();
        CardYearCombo = new javax.swing.JComboBox();
        VisaRadio = new javax.swing.JRadioButton();
        MasterCardRadio = new javax.swing.JRadioButton();
        BillingPanel = new javax.swing.JPanel();
        BillingTitleLabel = new javax.swing.JLabel();
        StreetNoLabel = new javax.swing.JLabel();
        StreetNoText = new javax.swing.JTextField();
        AptLabel = new javax.swing.JLabel();
        AptText = new javax.swing.JTextField();
        StreetLabel = new javax.swing.JLabel();
        StreetText = new javax.swing.JTextField();
        CityLabel = new javax.swing.JLabel();
        CityText = new javax.swing.JTextField();
        PostalLabel = new javax.swing.JLabel();
        PostalText = new javax.swing.JTextField();
        ProvinceLabel = new javax.swing.JLabel();
        ProvinceSelect = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        TitlePanel = new javax.swing.JPanel();
        TitleLabel = new javax.swing.JLabel();
        AddButton = new javax.swing.JButton();
        ErrorLabel = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        CardPanel.setBackground(new java.awt.Color(255, 255, 255));
        CardPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        CardTitleLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        CardTitleLabel.setText("Credit Card Information");

        CardnumberLabel.setFont(new java.awt.Font("Tahoma", 0, 18));
        CardnumberLabel.setText("CC Number");

        CardNameLabel.setFont(new java.awt.Font("Tahoma", 0, 18));
        CardNameLabel.setText("Name on Card");

        CardSecurityLabel.setFont(new java.awt.Font("Tahoma", 0, 18));
        CardSecurityLabel.setText("Security Code");

        CardExpirationLabel.setFont(new java.awt.Font("Tahoma", 0, 18));
        CardExpirationLabel.setText("Expiration Date");

        CardNumberLabel.setFont(new java.awt.Font("Tahoma", 0, 18));

        CardNameField.setFont(new java.awt.Font("Tahoma", 0, 18));

        CardSecurityField.setFont(new java.awt.Font("Tahoma", 0, 18));

        CardMonthCombo.setFont(new java.awt.Font("Tahoma", 0, 18));
        CardMonthCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "01 Jan", "02 Feb", "03 Mar", "04 Apr", "05 May", "06 Jun", "07 Jul", "08 Aug", "09 Sep", "10 Oct", "11 Nov", "12 Dec" }));

        CardYearCombo.setFont(new java.awt.Font("Tahoma", 0, 18));
        CardYearCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018" }));

        VisaRadio.setText("VISA");
        VisaRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                VisaRadioActionPerformed(evt);
            }
        });

        MasterCardRadio.setText("MasterCard");
        MasterCardRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MasterCardRadioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout CardPanelLayout = new javax.swing.GroupLayout(CardPanel);
        CardPanel.setLayout(CardPanelLayout);
        CardPanelLayout.setHorizontalGroup(
            CardPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(CardPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(CardPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, CardPanelLayout.createSequentialGroup()
                        .addComponent(CardImageLabel)
                        .addGap(221, 221, 221))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, CardPanelLayout.createSequentialGroup()
                        .addGroup(CardPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(CardPanelLayout.createSequentialGroup()
                                .addGap(7, 7, 7)
                                .addGroup(CardPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(CardnumberLabel)
                                    .addComponent(CardNameLabel)
                                    .addComponent(CardSecurityLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(CardPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(CardNameField)
                                    .addComponent(CardNumberLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
                                    .addComponent(CardSecurityField, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(CardPanelLayout.createSequentialGroup()
                                .addGap(97, 97, 97)
                                .addComponent(VisaRadio)
                                .addGap(18, 18, 18)
                                .addComponent(MasterCardRadio)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(CardPanelLayout.createSequentialGroup()
                                .addComponent(CardExpirationLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(CardMonthCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(CardYearCombo, 0, 94, Short.MAX_VALUE)
                                .addGap(5, 5, 5)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, CardPanelLayout.createSequentialGroup()
                        .addComponent(CardTitleLabel)
                        .addGap(37, 37, 37))))
        );
        CardPanelLayout.setVerticalGroup(
            CardPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(CardPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(CardTitleLabel)
                .addGap(41, 41, 41)
                .addGroup(CardPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(VisaRadio)
                    .addComponent(MasterCardRadio))
                .addGap(18, 18, 18)
                .addGroup(CardPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CardnumberLabel)
                    .addComponent(CardNumberLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(CardPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CardNameLabel)
                    .addComponent(CardNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(CardPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CardSecurityLabel)
                    .addComponent(CardSecurityField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(CardPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CardExpirationLabel)
                    .addComponent(CardMonthCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CardYearCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(CardImageLabel)
                .addContainerGap(157, Short.MAX_VALUE))
        );

        BillingPanel.setBackground(new java.awt.Color(255, 255, 255));
        BillingPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        BillingTitleLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        BillingTitleLabel.setText("Billing Address");

        StreetNoLabel.setFont(new java.awt.Font("Tahoma", 0, 18));
        StreetNoLabel.setText("Street Number");

        StreetNoText.setFont(new java.awt.Font("Tahoma", 0, 18));

        AptLabel.setFont(new java.awt.Font("Tahoma", 0, 18));
        AptLabel.setText("Apt Number");

        AptText.setFont(new java.awt.Font("Tahoma", 0, 18));

        StreetLabel.setFont(new java.awt.Font("Tahoma", 0, 18));
        StreetLabel.setText("Street");

        StreetText.setFont(new java.awt.Font("Tahoma", 0, 18));

        CityLabel.setFont(new java.awt.Font("Tahoma", 0, 18));
        CityLabel.setText("City");

        CityText.setFont(new java.awt.Font("Tahoma", 0, 18));

        PostalLabel.setFont(new java.awt.Font("Tahoma", 0, 18));
        PostalLabel.setText("Postal Code");

        PostalText.setFont(new java.awt.Font("Tahoma", 0, 18));

        ProvinceLabel.setFont(new java.awt.Font("Tahoma", 0, 18));
        ProvinceLabel.setText("Province");

        ProvinceSelect.setEditable(true);
        ProvinceSelect.setFont(new java.awt.Font("Tahoma", 0, 18));
        ProvinceSelect.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Alberta", "British Columbia", "Manitoba", "New Brunswick", "Newfoundland and Labrador", "Nova Scotia", "Nunavut", "Ontario", "Prince Edward Island", "Quebec", "Saskatchewan", "Yukon" }));
        ProvinceSelect.setSelectedIndex(1);

        jButton1.setText("Use Shipping Address");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout BillingPanelLayout = new javax.swing.GroupLayout(BillingPanel);
        BillingPanel.setLayout(BillingPanelLayout);
        BillingPanelLayout.setHorizontalGroup(
            BillingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(BillingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addGroup(BillingPanelLayout.createSequentialGroup()
                    .addGap(43, 43, 43)
                    .addComponent(BillingTitleLabel)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1))
                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, BillingPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(BillingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(ProvinceLabel)
                        .addComponent(PostalLabel)
                        .addComponent(CityLabel)
                        .addComponent(StreetLabel)
                        .addComponent(AptLabel)
                        .addComponent(StreetNoLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(BillingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(ProvinceSelect, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(PostalText, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(CityText, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(StreetText, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(StreetNoText, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(AptText, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );
        BillingPanelLayout.setVerticalGroup(
            BillingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(BillingPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(BillingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BillingTitleLabel)
                    .addComponent(jButton1))
                .addGap(33, 33, 33)
                .addGroup(BillingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(StreetNoText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(StreetNoLabel))
                .addGap(26, 26, 26)
                .addGroup(BillingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(AptText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(AptLabel))
                .addGap(26, 26, 26)
                .addGroup(BillingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(StreetText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(StreetLabel))
                .addGap(27, 27, 27)
                .addGroup(BillingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CityText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CityLabel))
                .addGap(26, 26, 26)
                .addGroup(BillingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PostalText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(PostalLabel))
                .addGap(24, 24, 24)
                .addGroup(BillingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ProvinceSelect, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ProvinceLabel))
                .addContainerGap(60, Short.MAX_VALUE))
        );

        TitlePanel.setBackground(new java.awt.Color(255, 255, 255));

        TitleLabel.setFont(new java.awt.Font("Copperplate Gothic Bold", 2, 60));
        TitleLabel.setText("Payment");

        AddButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        AddButton.setText("Pay");
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });

        ErrorLabel.setFont(new java.awt.Font("Tahoma", 0, 18));

        javax.swing.GroupLayout TitlePanelLayout = new javax.swing.GroupLayout(TitlePanel);
        TitlePanel.setLayout(TitlePanelLayout);
        TitlePanelLayout.setHorizontalGroup(
            TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(TitlePanelLayout.createSequentialGroup()
                .addComponent(TitleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(ErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        TitlePanelLayout.setVerticalGroup(
            TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(TitlePanelLayout.createSequentialGroup()
                .addGroup(TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(TitlePanelLayout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(ErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(TitlePanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(TitleLabel))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(CardPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BillingPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(TitlePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(BillingPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(CardPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(TitlePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed
        // TODO add your handling code here:


        //validation
        if (validateFields()) {
            if (cardSelected){
                Payment pymnt = new Payment(CardNumberLabel.getText(), cardType, desktopController.getSessionMgr().getCart().getTotal());
                desktopController.makePayment(pymnt);
            }
            else{
                ErrorLabel.setText("Please choose a card type");
            }
        } else {
            ErrorLabel.setVisible(true);
        }
    }//GEN-LAST:event_AddButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Address addr = desktopController.getSessionMgr().getShipaddressForOrder();
        StreetNoText.setText(Integer.toString(addr.getStNo()));
        AptText.setText(addr.getAptNo());
        StreetText.setText(addr.getStreet());
        CityText.setText(addr.getCity());
        PostalText.setText(addr.getPostCode());

    }//GEN-LAST:event_jButton1ActionPerformed

    private void MasterCardRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MasterCardRadioActionPerformed
        if(!cardSelected){
            cardSelected = true;
        }
        if(VisaRadio.isSelected()){
            VisaRadio.setSelected(false);
        }
        cardType = "MasterCard";

    }//GEN-LAST:event_MasterCardRadioActionPerformed

    private void VisaRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_VisaRadioActionPerformed
        if(!cardSelected){
            cardSelected = true;
        }
        if(MasterCardRadio.isSelected()){
            MasterCardRadio.setSelected(false);
        }
        cardType = "VISA";
    }//GEN-LAST:event_VisaRadioActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddButton;
    private javax.swing.JLabel AptLabel;
    private javax.swing.JTextField AptText;
    private javax.swing.JPanel BillingPanel;
    private javax.swing.JLabel BillingTitleLabel;
    private javax.swing.JLabel CardExpirationLabel;
    private javax.swing.JLabel CardImageLabel;
    private javax.swing.JComboBox CardMonthCombo;
    private javax.swing.JTextField CardNameField;
    private javax.swing.JLabel CardNameLabel;
    private javax.swing.JTextField CardNumberLabel;
    private javax.swing.JPanel CardPanel;
    private javax.swing.JTextField CardSecurityField;
    private javax.swing.JLabel CardSecurityLabel;
    private javax.swing.JLabel CardTitleLabel;
    private javax.swing.JComboBox CardYearCombo;
    private javax.swing.JLabel CardnumberLabel;
    private javax.swing.JLabel CityLabel;
    private javax.swing.JTextField CityText;
    private javax.swing.JLabel ErrorLabel;
    private javax.swing.JRadioButton MasterCardRadio;
    private javax.swing.JLabel PostalLabel;
    private javax.swing.JTextField PostalText;
    private javax.swing.JLabel ProvinceLabel;
    private javax.swing.JComboBox ProvinceSelect;
    private javax.swing.JLabel StreetLabel;
    private javax.swing.JLabel StreetNoLabel;
    private javax.swing.JTextField StreetNoText;
    private javax.swing.JTextField StreetText;
    private javax.swing.JLabel TitleLabel;
    private javax.swing.JPanel TitlePanel;
    private javax.swing.JRadioButton VisaRadio;
    private javax.swing.JButton jButton1;
    // End of variables declaration//GEN-END:variables

    private boolean validateFields() {

        boolean isValid = true;
        //credit card validations
        if ("".equalsIgnoreCase(CardNumberLabel.getText().trim())) {
            isValid = false;
            ErrorLabel.setText("credit card number field is mandatory");
        } else if (CardNumberLabel.getText().length() != 16) {
            isValid = false;
            ErrorLabel.setText("credit card number should be 16 digits");
        } else if ("".equalsIgnoreCase(CardNameField.getText().trim())) {
            isValid = false;
            ErrorLabel.setText("card name field is mandatory");
        } else if ("".equalsIgnoreCase(CardSecurityField.getText().trim())) {
            isValid = false;
            ErrorLabel.setText("card security code  is mandatory");
        } else if (CardSecurityField.getText().length() != 3) {
            isValid = false;
            ErrorLabel.setText("card security code  should be 3 digits");
        } else if (CardMonthCombo.getSelectedIndex() == -1) {
            isValid = false;
            ErrorLabel.setText("please select expiry date");
        } //billing info validation
        else if ("".equalsIgnoreCase(StreetNoText.getText().trim())) {
            isValid = false;
            ErrorLabel.setText("street number field is mandatory");
        }else if ("".equalsIgnoreCase(StreetText.getText().trim())) {
            isValid = false;
            ErrorLabel.setText("street field is mandatory");
        } else if ("".equalsIgnoreCase(CityText.getText().trim())) {
            isValid = false;
            ErrorLabel.setText("city field is mandatory");
        } else if ("".equalsIgnoreCase(PostalText.getText().trim())) {
            isValid = false;
            ErrorLabel.setText("postal code  field is mandatory");
        } else if (ProvinceSelect.getSelectedIndex() == -1) {
            isValid = false;
            ErrorLabel.setText("please select province");
        } //postal code validtion
        else if (!"".equalsIgnoreCase(PostalText.getText().trim())) {

            if (PostalText.getText().length() != 6) {
                isValid = false;
                ErrorLabel.setText("Less than 6");
            }

            String option = (String) ProvinceSelect.getSelectedItem();

            char[] postal = PostalText.getText().toUpperCase().toCharArray();

            int ascii1 = PostalText.getText().charAt(1);
            int ascii2 = PostalText.getText().toUpperCase().charAt(2);
            int ascii3 = PostalText.getText().charAt(3);
            int ascii4 = PostalText.getText().toUpperCase().charAt(4);
            int ascii5 = PostalText.getText().charAt(5);

            if (ascii1 < 48 || ascii1 > 57) {
                isValid = false;//Is Number
                ErrorLabel.setText("Postal Code Invalid");
            } else if (ascii2 < 65 || ascii2 > 90) {
                isValid = false;
                ErrorLabel.setText("Postal Code Invalid");
            } else if (ascii3 < 48 || ascii3 > 57) {
                isValid = false;
                ErrorLabel.setText("Postal Code Invalid");
            } else if (ascii5 < 48 || ascii5 > 57) {
                isValid = false;
                ErrorLabel.setText("Postal Code Invalid");
            } else if (ascii4 < 65 || ascii4 > 90) {
                isValid = false;
                ErrorLabel.setText("Postal Code Invalid");
            } else {
                ErrorLabel.setText("");
            }

            if (postal[0] == 'A' || postal[0] == 'B' || postal[0] == 'C' || postal[0] == 'E' || postal[0] == 'G' || postal[0] == 'H'
                    || postal[0] == 'J' || postal[0] == 'K' || postal[0] == 'L' || postal[0] == 'M' || postal[0] == 'N' || postal[0] == 'P'
                    || postal[0] == 'R' || postal[0] == 'S' || postal[0] == 'T' || postal[0] == 'V' || postal[0] == 'X' || postal[0] == 'Y') {
                if (postal[0] == 'A' && option.equals("Newfoundland and Labrador") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                } else if (postal[0] == 'B' && option.equals("Nova Scotia") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                } else if (postal[0] == 'C' && option.equals("Prince Edward Island") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                } else if (postal[0] == 'E' && option.equals("New Brunswick") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                } else if (postal[0] == 'G' && option.equals("Quebec") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                } else if (postal[0] == 'H' && option.equals("Quebec") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                } else if (postal[0] == 'J' && option.equals("Quebec") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                } else if (postal[0] == 'K' && option.equals("Ontario") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                } else if (postal[0] == 'L' && option.equals("Ontario") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                } else if (postal[0] == 'M' && option.equals("Ontario") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                } else if (postal[0] == 'N' && option.equals("Ontario") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                } else if (postal[0] == 'P' && option.equals("Ontario") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                } else if (postal[0] == 'R' && option.equals("Manitoba") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                } else if (postal[0] == 'S' && option.equals("Saskatchewan") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                } else if (postal[0] == 'T' && option.equals("Alberta") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                } else if (postal[0] == 'V' && option.equals("British Columbia") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                } else if (postal[0] == 'X' && option.equals("Northwest Territories") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                } else if (postal[0] == 'X' && option.equals("Nunavut") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                } else if (postal[0] == 'Y' && option.equals("Yukon") == false) {
                    isValid = false;
                    ErrorLabel.setText("Postal Code Invalid");
                }
            } else {
                isValid = false;
                ErrorLabel.setText("Postal Code Invalid");
            }

        }
        return isValid;
    }
}
